﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class frmPhanQuyen : Form
    {
        public frmPhanQuyen()
        {
            InitializeComponent();
        }
        List<CHUCVUDTO> lstCV;
        List<QUYENHANDTO> lstQH;
      
        private void loadDTGV()
        {
            CHUCVUBUS CvBUS = new CHUCVUBUS();
            lstCV = new List<CHUCVUDTO>();
            lstCV = CvBUS.DSChucVu();

            QUYENHANBUS QhBUS = new QUYENHANBUS();

            for (int i = 0; i < lstCV.Count; i++)
            {
                dtgvPhanQuyen.Rows.Add(lstCV[i].MaCV, lstCV[i].TenCV);
                for (int j = 2; j < dtgvPhanQuyen.ColumnCount; j++)
                    dtgvPhanQuyen.Rows[i].Cells[j].Value = false;
                dtgvPhanQuyen.Rows[i].Cells["doiMK"].Value = true;
                lstQH = new List<QUYENHANDTO>();
                lstQH = QhBUS.LayDSQH(lstCV[i]);
                for (int j = 0; j < lstQH.Count; j++)
                {
                    if (lstQH[j].QH < dtgvPhanQuyen.ColumnCount - 1)
                        dtgvPhanQuyen.Rows[i].Cells[lstQH[j].QH+1].Value = true;
                }
            }
        }
        
        private void frmPhanQuyen_Load(object sender, EventArgs e)
        {
            loadDTGV();
            //MessageBox.Show(dtgvPhanQuyen.ColumnCount.ToString());
        }

      
        private void bntCapNhat_Click(object sender, EventArgs e)
        {            
            if (MessageBox.Show("Bạn có chắc muốn lưu lại những thay đổi?",
                "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.No) return;
            int rowc = dtgvPhanQuyen.RowCount;
            int colc = dtgvPhanQuyen.ColumnCount;
            if (rowc < 2) return;
            QUYENHANBUS QhBUS = new QUYENHANBUS();
            for (int i = 0; i < rowc; i++)
            {
                QUYENHANDTO QhDTO = new QUYENHANDTO();
                QhDTO.MaCV = int.Parse( dtgvPhanQuyen.Rows[i].Cells["cMaCV"].Value.ToString());
                for (int j = 2; j < colc; j++)
                {
                    QhDTO.QH = j-1;
                    if (dtgvPhanQuyen.Rows[i].Cells[j].Value.ToString() == "True")
                        QhBUS.ThemQH(QhDTO, 1);
                    else 
                        QhBUS.ThemQH(QhDTO, 0);
                }
            }

            MessageBox.Show("Đã cập nhật xong!");
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtgvPhanQuyen_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int dong = dtgvPhanQuyen.CurrentRow.Index;
            int cot = dtgvPhanQuyen.CurrentCell.ColumnIndex;
            //MessageBox.Show("Dong " + dong + "\n Cột "+cot+" " +
            //    dtgvPhanQuyen.CurrentCell.Value.ToString());
        }
    }
}
